<?php

Class Guru extends CI_Controller {

//constructor
    function __construct() {
        parent::__construct();
        $this->load->model('model_user');
        if ($this->model_user->auth() == 0 || $this->model_user->auth() !== 1) {
            redirect('login/index');
        }else{
            if($this->session->userdata('type') == 'admin'){
                redirect('admin');
            }else if($this->session->userdata('type') == 'siswa'){
                redirect('siswa');
            }
        }
        $this->load->model('Model_soal');
        $this->load->model('model_nilai');
    }

    function index() {
        $data['nama'] = $this->session->userdata['nama'];
        $data['main_content'] = 'guru/index';
        $this->load->view('includes/template', $data);
    }

    function lihatsoal() {
        $data['main_content'] = 'guru/daftar';
        $id = $this->session->userdata['id'];
        $data['query'] = $this->Model_soal->getSoal($id);
        $this->load->view('includes/template', $data);
    }

    function buatsoal() {
        $data['main_content'] = 'guru/buat_soal';
        $data['nama'] = $this->session->userdata['nama'];
        $data['id'] = $this->session->userdata['id'];
        $data['query1'] = $this->Model_soal->getTypeSoal();
        //$data['query'] = $this->Model_guru->getAll();
        $this->load->view('includes/template', $data);
    }

    function soalsubmit() {
        if ($this->input->post('submit')) {

            if ($this->input->post('id')) {
                $this->form_validation->set_rules('kelas', 'Kelas', 'required');
                $this->form_validation->set_rules('materi', 'materi', 'required');
                $this->form_validation->set_rules('type_soal', 'Type soal', 'required');
                $this->form_validation->set_rules('jml_soal', 'Jumlah Soal', 'required');
                $this->form_validation->set_rules('materi', 'materi', 'required');
                $this->form_validation->set_rules('type_soal', 'Type soal', 'required');
                if ($this->form_validation->run() == FALSE) {
                    $result = $this->Model_soal->getEditSoal($this->input->post('id'));
                    $data['id'] = $result['id'];
                    $data['kelas'] = $result['kelas'];
                    $data['materi'] = $result['materi'];
                    $data['id_type_soal'] = $result['id_type_soal'];
                    $data['jumlah_soal'] = $result['jumlah_soal'];
                    $data['jumlah_opsi'] = $result['jumlah_opsi'];
                    $data['mode_acak'] = $result['mode_acak'];
                    $data['query1'] = $this->Model_soal->getTypeSoal();
                    $data['main_content'] = 'guru/edit_soal';
                    $this->load->view('includes/template', $data);
                } else {
                    $this->Model_soal->updateSoal();
                    redirect('guru/lihatsoal');
                    //redirect('guru/index');
                }
            } else {
                $this->form_validation->set_rules('id_guru', 'ID GURU', 'required');
                $this->form_validation->set_rules('kelas', 'Kelas', 'required');
                $this->form_validation->set_rules('materi', 'materi', 'required');
                $this->form_validation->set_rules('type_soal', 'Type soal', 'required');
                $this->form_validation->set_rules('jml_soal', 'Jumlah Soal', 'required');
                $this->form_validation->set_rules('materi', 'materi', 'required');
                $this->form_validation->set_rules('type_soal', 'Type soal', 'required');
                $this->form_validation->set_rules('waktu', 'Waktu', 'required');

                if ($this->form_validation->run() == FALSE) {
                    //$data['query'] = $this->Model_guru->getAll();
                    $this->buatsoal();
                } else {
                    $this->Model_soal->soalsave();
                    redirect('guru/buatdetsoal');
                }
            }
        }
    }

    function editsoal() {
        $id = $this->uri->segment(3);
        $data['result'] = $this->Model_soal->getEditSoal($id);

        if (empty($id) or count($data['result']) == 0) {
            redirect('guru/lihatsoal');
        } else {
            $result = $this->Model_soal->getEditSoal($id);
            $data['id'] = $result['id'];
            $data['kelas'] = $result['kelas'];
            $data['materi'] = $result['materi'];
            $data['id_type_soal'] = $result['id_type_soal'];
            $data['jumlah_soal'] = $result['jumlah_soal'];
            $data['jumlah_opsi'] = $result['jumlah_opsi'];
            $data['mode_acak'] = $result['mode_acak'];
            $data['waktu'] = $result['waktu'];
            $data['query1'] = $this->Model_soal->getTypeSoal();
            $data['main_content'] = 'guru/edit_soal';
            $this->load->view('includes/template', $data);
        }
    }
    
    function detsoallaunch(){
        $id = $this->uri->segment(3);
        $data['jumlah'] = $this->Model_soal->countjumsoal($id);
        $data['rows'] = $this->Model_soal->retrievedetsoal($id);
        $data['main_content'] = 'guru/detsoalshow';
        $this->session->set_userdata('id_soal', $id);
        $this->load->view('includes/template', $data);
    }
    
    function editDetailsoal() {
        $id = $this->uri->segment(3);
        $data['result'] = $this->Model_soal->getDetailSoal($id);

        if (empty($id) or count($data['result']) == 0) {
            redirect('guru/lihatsoal');
        } else {
            $result = $this->Model_soal->getDetailSoal($id);
            $data['id'] = $result['id'];
            $data['id_soal'] = $result['id_soal'];
            $data['soal'] = $result['soal'];
            $data['gambarsoal'] = $result['gambarsoal'];
            $data['opsia'] = $result['opsia'];
            $data['gambara'] = $result['gambara'];
            $data['opsib'] = $result['opsib'];
            $data['gambarb'] = $result['gambarb'];
            $data['opsic'] = $result['opsic'];
            $data['gambarc'] = $result['gambarc'];
            $data['opsid'] = $result['opsid'];
            $data['gambard'] = $result['gambard'];
            $data['opsie'] = $result['opsie'];
            $data['gambare'] = $result['gambare'];
            $data['kunci'] = $result['kunci'];
            $data['main_content'] = 'guru/edit_detail_soal';
            $opsi = $this->Model_soal->countjumsoal($data['id_soal']);
            $this->session->set_userdata('opsi', $opsi->jumlah_opsi);
            $this->load->view('includes/template', $data);
        }
        
     
    }

    function buatdetsoal() {
        $this->load->model('model_soal');
        $data = $this->model_soal->countdetsoal();
        if($data['end'] == true){
            redirect('guru/lihatsoal');
        }else{
            $data['main_content'] = 'guru/detail_soal';
            $data['id_soal'] = $this->session->userdata('id_soal');
            $this->load->view('includes/template', $data);
        }
    }

    function detsoalsubmit() {
        $this->form_validation->set_rules('pertanyaan', 'Pertanyaan', 'trim|required');
        $this->form_validation->set_rules('opsi1', 'Opsi 1', 'trim|required');
        $this->form_validation->set_rules('kunci', 'Kunci', 'trim|required');
        if ($this->form_validation->run() == false) {
            $this->buatdetsoal();
        } else {
            $this->load->model('model_soal');
            $this->model_soal->insertdetsoal();
            redirect('guru/buatdetsoal');
        }
    }
    
    function editdetsoalsubmit(){
        $this->form_validation->set_rules('pertanyaan', 'Pertanyaan', 'trim|required');
        $this->form_validation->set_rules('opsi1', 'Opsi 1', 'trim|required');
        if ($this->form_validation->run() == false) {
            redirect('guru/lihatsoal');
        } else {
            $this->Model_soal->editdetsoal();
        }
    }
    
    function delconfsoal() {
        $id = $this->uri->segment(3);
        $data['result'] = $this->Model_soal->getEditSoal($id);

        if (empty($id) or count($data['result']) == 0) {
            redirect('guru/lihatsoal');
        } else {
            $result = $this->Model_soal->getEditSoal($id);
            $data['materi'] = $result['materi'];
            $data['id'] = $result['id'];
            $this->load->view('guru/delconfsoal', $data);
        }
    }
    
    function delsoal($id) {
        //$this->db->delete('soal', array('id' => $id));
        $this->db->update('soal', array('is_deleted' => '1'), array('id' => $id));
        echo "<script type='text/javascript'>window.top.window.alertz('alert-success','Sukses !', 'Data berhasil dihapus');</script>";
        //$this->load->view('admin/view_guru/show', $data);
    }
    
    function showsoal(){
        $id = $this->session->userdata['id'];
        $data['query'] = $this->Model_soal->getSoal($id);
        $this->load->view('guru/show', $data);
    }
    
    function nilai(){
        $id = $this->session->userdata['id'];
        $data['query'] = $this->Model_soal->getSoal($id);
        $data['main_content'] = 'guru/show1';
        $this->load->view('includes/template', $data);
    }
    
    function daftarnilai($id){
        $data['nilais'] = $this->model_nilai->getDaftarNilai($id);
        $data['main_content'] = 'guru/daftar_nilai';
        $this->load->view('includes/template', $data);
    }

}

?>